<?php

declare(strict_types=1);

namespace app\model;

use think\Model;

/**
 * @mixin \think\Model
 */
class BaseModel extends Model
{
    // 只读字段 不允许更新
    protected $readonly = ['id'];

    protected $autoWriteTimestamp = true;
    //软删除
    //是否为软删除
    public $softDelete = false;
    public $deleteTime = 'delete_time';
    public $defaultSoftDelete = 0;

    //聚合可搜索字段
    protected $searchField = [];
    //单独可搜索字段
    protected $singleSearchField = [];
    //可作为条件的字段
    protected $whereField = [];

    //whereIn条件
    protected $whereInField = [];

    //可做为时间范围查询的字段
    protected $timeField = ['created_at', 'created_time', 'create_time'];
    //禁止删除的数据id
    public $noDeletionId = [];



    //时间返回格式化
    public function getCreatedAtAttr($value, $data)
    {
        if (isset($data['created_at']) && $data['created_at']) {
            return date("Y-m-d H:i:s", (int)$value);
        }
    }
    public function getUpdatedAtAttr($value, $data)
    {
        if (isset($data['updated_at']) && $data['updated_at']) {
            return date("Y-m-d H:i:s", (int)$value);
        }
    }


    /**
     * 查询处理
     * @var Query $query
     * @var array $param
     */
    public function scopeWhere($query, $param): void
    {
        if (is_null($param)) {
            $param = [];
        }

        //关键词like搜索
        $keywords = $param['keywords'] ?? '';
        if ('' !== $keywords && count($this->searchField) > 0) {
            $this->searchField = implode('|', $this->searchField);
            $query->where($this->searchField, 'like', '%' . $keywords . '%');
        }
        //字段条件查询
        if (count($this->whereField) > 0 && count($param) > 0) {
            foreach ($param as $key => $value) {
                if ($value !== '' && in_array((string)$key, $this->whereField, true)) {
                    $query->where($key, $value);
                }
            }
        }

        //where in 查询
        if (count($this->whereInField) > 0 && count($param) > 0) {
            foreach ($param as $key => $value) {

                if ($value !== '' && in_array((string)$key, $this->whereInField, true)) {
                    if (is_string($value)) {
                        $values = explode(' ', $value);
                        // storage_log("values" . json_encode($values));
                    } else {
                        $values = $value;
                    }
                    $query->whereIn($key, $values);
                }
            }
        }

        //20221117单独like查询
        if (count($this->singleSearchField) > 0 && count($param) > 0) {
            foreach ($param as $key => $value) {
                // 20201014 单独可搜索字段 like查询
                if ($value !== '' && in_array((string)$key, $this->singleSearchField, true)) {
                    $query->where($key, 'like', '%' . trim($value) . '%');
                }
            }
        }

        //时间范围查询
        // if (count($this->timeField) > 0 && count($param) > 0) {
        //     foreach ($param as $key => $value) {
        //         if ($value !== '' && in_array((string)$key, $this->timeField, true)) {
        //             //$field_type = $this->getFieldsType($this->table, $key);
        //             $time_range = explode(' - ', $value);
        //             [$start_time, $end_time] = $time_range;
        //             //如果是int，进行转换
        //             //if (false !== strpos($field_type, 'int')) {
        //             $start_time = strtotime($start_time);
        //             if (strlen($end_time) === 10) {
        //                 $end_time .= '23:59:59';
        //             }
        //             $end_time = strtotime($end_time);
        //             //                    }
        //             $query->whereBetweenTime($key, $start_time, $end_time);
        //         }
        //     }
        // }
        if (count($this->timeField) > 0 && count($param) > 0) {
            foreach ($param as $key => $value) {
                if ($value !== '' && in_array((string)$key, $this->timeField, true)) {
                    //$field_type = $this->getFieldsType($this->table, $key);
                    if (is_array($value)) {
                        // 数组
                        $start_time = $value[0];
                        $end_time = $value[1];
                        // 兼容毫秒
                        if (strlen($start_time) == 13) {
                            $start_time = $start_time / 1000;
                        }
                        if (strlen($end_time) == 13) {
                            $end_time = $end_time / 1000;
                        }
                    } else {
                        $time_range = explode(' - ', $value);
                        [$start_time, $end_time] = $time_range;
                        //如果是int，进行转换
                        //if (false !== strpos($field_type, 'int')) {
                        $start_time = strtotime($start_time);
                        if (strlen($end_time) === 10) {
                            $end_time .= '23:59:59';
                        }
                        $end_time = strtotime($end_time);
                    }

                    $query->whereBetweenTime($key, $start_time, $end_time);
                }
            }
        }
        //排序
        //$order = $param['_order'] ?? '';
        //$by    = $param['_by'] ?? 'desc';
        //$query->order($order ?: 'id', $by ?: 'desc');
    }
}
